package org.dtrd.modules.patient.job;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.modules.patient.service.IDtrdEntRdReportService;
import org.dtrd.modules.service.service.IDtrdEntRdServicePackageService;
import org.dtrd.modules.service.service.IDtrdRlPackagePatientService;
import org.dtrd.modules.util.ReportUtil;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author qr
 * @date 2022/1/22 17:33
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Component
@Slf4j
public class ReportWeekJob implements Job {

    @Autowired
    private IDtrdEntRdReportService reportService;
    @Autowired
    private IDtrdEntRdServicePackageService packageService;
    @Autowired
    private IDtrdRlPackagePatientService packagePatientService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<Integer> patientIds = packagePatientService.getServicingPatientIds();
        log.info("ReportWeekJob.execute.[context] patientIds=" + patientIds);
        if (CollectionUtil.isNotEmpty(patientIds)) {
            // 生成周报
            for (Integer patientId : patientIds) {
                // 报告名字
                String packageName = packageService.getServingPackageName(patientId);
                if (packageName == null) {
                    return;
                }
                String reportName = ReportUtil.getReportWeekName(packageName);
                log.info("ReportWeekJob.execute.[context] packaheName=" + packageName + ";reportName=" + reportName);
                reportService.createReportWeek(patientId, reportName);
                log.info("----------患者: {} 生成周报: {}----------", patientId, reportName);
            }
        }
    }

}
